<template>
  <el-dialog
    title="分配权限"
    :visible="visible"
    width="50%"
    @close="onDialogClose"
  >
    <el-tree
      ref="tree"
      :data="permissionTreeData"
      show-checkbox
      node-key="id"
      default-expand-all
      :default-checked-keys="currentPermissionId"
      :props="defaultProps"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="onDialogClose">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { distributePermissions } from '@/api/role'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    permissionTreeData: {
      type: Array,
      required: true
    },
    currentPermissionId: {
      type: Array,
      required: true
    },
    roleId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },
  created() {

  },

  methods: {
    // 对话框关闭，数据重置
    onDialogClose() {
      this.$refs.tree.setCheckedKeys([])
      this.$emit('input', false)
    },
    // 点击确定,发送请求修改权限
    async onSave() {
      const rids = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()).join()
      await distributePermissions(this.roleId, { rids })
      this.$message.success('更新权限成功！')
      this.$emit('success')
      this.onDialogClose()
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
